#include "TTP229.h"

String item;
String item_1;
String i;
String Read_Key_a = "";
volatile int Password_flag;
volatile int Key_flag;
volatile int number;
volatile int number_1 = 0;
volatile int number_2 = 0;
String Write_password[]={" "," "," "};
String Save_password[]={"1","2","3"};
String Password_list[]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","*","#"};
 
ST_KEY_MAP em_ttp_keymap[16] = {
  {"1", 0xFFFE},
  {"2", 0xFFFD},
  {"3", 0xFFFB},
  {"4", 0xFFEF},
  {"5", 0xFFDF},
  {"6", 0xFFBF},
  {"7", 0xFEFF},
  {"8", 0xFDFF},
  {"9", 0xFBFF},
  {"*", 0xEFFF},
  {"0", 0xDFFF},
  {"#", 0xBFFF},
  {"D", 0x7FFF},
  {"C", 0xF7FF},
  {"B", 0xFF7F},
  {"A", 0xFFF7},
};

void TTP229::initTTP229(uint8_t SclPin, uint8_t SdoPin) {
  SCLPin = SclPin;
  SDOPin = SdoPin;
  pinMode(SCLPin , OUTPUT);
  pinMode(SDOPin , INPUT);
}

uint16_t TTP229::GetKeyCode(void) {
  unsigned int DATA = 0;
  pinMode(SDOPin , OUTPUT);
  digitalWrite(SDOPin, HIGH);
  delayMicroseconds(93);
  digitalWrite(SDOPin, LOW);
  delayMicroseconds(10);
  pinMode(SDOPin , INPUT);
  for (int i = 0; i < 16; i++)
  {
    digitalWrite(SCLPin, HIGH);
    digitalWrite(SCLPin, LOW);
    DATA |= digitalRead(SDOPin) << i;
  }
  delay(4);
  return DATA & 0xFFFF;
}
String TTP229::GetKeyMap(void) {
  byte i;
  uint16_t keycode = GetKeyCode();
  ST_KEY_MAP *irkeymap = em_ttp_keymap;
  for (i = 0; i < KEY_MAX; i++) {
    //    Serial.println(irkeymap[i].keycode);
    if (irkeymap[i].keycode == keycode)
      return irkeymap[i].keyname;
  }
  return "";
}

uint16_t TTP229::GetKey(void) {
  byte i;
  uint16_t keycode = GetKeyCode();
  ST_KEY_MAP *irkeymap = em_ttp_keymap;
  for (i = 0; i < KEY_MAX; i++) {
    //    Serial.println(irkeymap[i].keycode);
    if (irkeymap[i].keycode == keycode)
      return i;
  }
  return 0xff;
}

String  TTP229::Read_Key (void) {
 String key_name = GetKeyMap();
 char * result = (char *)key_name.c_str();
 return result;
}

void  TTP229::Set_password(KEY set_password_0, KEY set_password_1, KEY set_password_2){
  Save_password[0] = Password_list[set_password_0];
  Save_password[1] = Password_list[set_password_1];
  Save_password[2] = Password_list[set_password_2];
  delay(100);
}

int TTP229::Output_key(KEY task){
  switch (task){
    
    case 0: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 1: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
    case 2: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 3: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 4: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 5: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 6: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 7: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 8: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 9: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 10: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
             
    case 11: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            } 
             
    case 12: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
            
    case 13: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
             
    case 14: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }
             
    case 15: item_1 = Read_Key();
            if( item_1 == Password_list[task] ){
               number_2 = 1;
               return 1;
            }
            if(number_2 == 1 && item_1 == ""){
               number_2 = 0;
               return 0;
            }                                                                                                                                                      
   }
                  
}

int TTP229::Password(void){
  String password_0 = Save_password[0];
  String password_1 = Save_password[1];
  String password_2 = Save_password[2];
  if(number == 0){
    item = Read_Key();
      if(item != "" && number_1 == 0){
        i = item;
        number_1 = 1;
      }
    if(item == "" && number_1 == 1 ){
     Serial.print(i);
     Write_password[0] = i;
     number_1 = 0;
     number = 1;
     delay(50);
    }
  }

  if(number == 1){
    item = Read_Key();
      if(item != "" && number_1 == 0){
        i = item;
        number_1 = 1;
      }
    if(item == "" && number_1 == 1 ){
     Serial.print(i);
     Write_password[1] = i;
     number_1 = 0;
     number = 2;
     delay(50);
    }
  }

  if(number == 2){
    item = Read_Key();
      if(item != "" && number_1 == 0){
        i = item;
        number_1 = 1;
      }
    if(item == "" && number_1 == 1 ){
     Serial.println(i);
     Write_password[2] = i;
     number_1 = 0;
     number = 3;
     delay(50);
    }
  }
   if(number == 3 && Write_password[0] == password_0 && Write_password[1] == password_1 && Write_password[2] == password_2){
     delay(100);
     Write_password[0] = " ";
     Write_password[1] = " ";
     Write_password[2] = " ";
     number = 0;
     Password_flag = 1;  
     }
   else if(number == 3 && (Write_password[0] != password_0 || Write_password[1] != password_1 || Write_password[2] != password_2)){
     delay(100);
     Write_password[0] = " ";
     Write_password[1] = " ";
     Write_password[2] = " ";
     number = 0;
     Password_flag = 2;
     }
   else{
     Password_flag = 0; 
   }
   return Password_flag;
}
